﻿using System.Threading;
using System.Windows.Forms;

namespace KWBridge
{
    public partial class KJSysUserControl2 : UserControl
    {
        public KJSysUserControl2( )
        {
            InitializeComponent( );
        }

        private Eflong.Config.KWBridgeConfig cfg = Eflong.Config.KWBridgeConfig.Instance;

        private SynchronizationContext syncContext;

        /// <summary>
        /// 更新显示内容
        /// </summary>
        public void UpdateText( )
        {
            //WaitCallback d = new WaitCallback(UpdateParas);
            //ThreadPool.QueueUserWorkItem(d, null);

            UpdateParas( null );
        }

        private string strCoalMineName = string.Empty;

        private string strServerIp = string.Empty;

        private long nId = 0;

        protected int nInterval = 10;

        public int nBreakInterval = 3;

        private string strKJType = string.Empty;

        private string strPath = string.Empty;

        private string strDevFileName = string.Empty;

        private string strRtDataName = string.Empty;

        private string strKJType2 = string.Empty;

        private string strPath2 = string.Empty;

        private string strDevFileName2 = string.Empty;

        private string strRtDataName2 = string.Empty;

        private string strPostfix = string.Empty;

        private string strPostfix2 = string.Empty;

        public string ServerIp
        {
            get { return strServerIp; }
            set { strServerIp = value; }
        }

        public string CoalMineName
        {
            get { return strCoalMineName; }
            set { strCoalMineName = value; }
        }

        public long CoalMineId
        {
            get { return nId; }
            set { nId = value; }
        }

        public string KJType
        {
            get { return strKJType; }
            set { strKJType = value; }
        }

        public int Interval
        {
            get { return nInterval; }
            set { nInterval = value; }
        }

        public string Path
        {
            get { return strPath; }
            set { strPath = value; }
        }

        public string DevFileName
        {
            get { return strDevFileName; }
            set { strDevFileName = value; }
        }

        public string RtDataName
        {
            get { return strRtDataName; }
            set { strRtDataName = value; }
        }

        public string Postfix
        {
            get { return strPostfix; }
            set { strPostfix = value; }
        }

        public string Postfix2
        {
            get { return strPostfix2; }
            set { strPostfix2 = value; }
        }

        public string KJType2
        {
            get { return strKJType2; }
            set { strKJType2 = value; }
        }

        public string Path2
        {
            get { return strPath2; }
            set { strPath2 = value; }
        }

        public string DevFileName2
        {
            get { return strDevFileName2; }
            set { strDevFileName2 = value; }
        }

        public string RtDataName2
        {
            get { return strRtDataName2; }
            set { strRtDataName2 = value; }
        }

        public void UpdateParas( object state )
        {
            //if (tbCoalMineName.InvokeRequired)
            //{
            //    SetMessageCallback callback = new SetMessageCallback(this.Set_tbCoalMineNameText);
            //    tbCoalMineName.BeginInvoke(callback, new string[] { CoalMineName });
            //}
            //else
            //{
            //    tbCoalMineName.Text = CoalMineName;
            //}
            //if (tbCoalMineId.InvokeRequired)
            //{
            //    SetMessageCallback callback = new SetMessageCallback(this.Set_tbCoalMineIdText);
            //    tbCoalMineId.BeginInvoke(callback, new string[] { CoalMineId.ToString() });
            //}
            //else
            //{
            //    tbCoalMineId.Text = CoalMineId.ToString();
            //}
            //if (tbIPAddress.InvokeRequired)
            //{
            //    SetMessageCallback callback = new SetMessageCallback(this.Set_tbIPAddressText);
            //    tbIPAddress.BeginInvoke(callback, new string[] { ServerIp });
            //}
            //else
            //{
            //    tbIPAddress.Text = ServerIp;
            //}

            //if (tbInterval.InvokeRequired)
            //{
            //    SetMessageCallback callback = new SetMessageCallback(this.Set_tbIntervalText);
            //    tbInterval.BeginInvoke(callback, new string[] { nInterval.ToString() });
            //}
            //else
            //{
            //    tbInterval.Text = nInterval.ToString();
            //}

            //if (tbKJType.InvokeRequired)
            //{
            //    SetMessageCallback callback = new SetMessageCallback(this.Set_tbKJTypeText);
            //    tbKJType.BeginInvoke(callback, new string[] { KJType });
            //}
            //else
            //{
            //    tbKJType.Text = KJType;
            //}

            //if (tbKJType2.InvokeRequired)
            //{
            //    SetMessageCallback callback = new SetMessageCallback(this.Set_tbKJTypeText2);
            //    tbKJType2.BeginInvoke(callback, new string[] { KJType2 });
            //}
            //else
            //{
            //    tbKJType2.Text = KJType2;
            //}

            //if (tbDevFile.InvokeRequired)
            //{
            //    SetMessageCallback callback = new SetMessageCallback(this.Set_tbDevFileText);
            //    tbDevFile.BeginInvoke(callback, new string[] { DevFileName });
            //}
            //else
            //{
            //    tbDevFile.Text = DevFileName;
            //}
            //if (tbRtData.InvokeRequired)
            //{
            //    SetMessageCallback callback = new SetMessageCallback(this.Set_tbRtDataText);
            //    tbRtData.BeginInvoke(callback, new string[] { RtDataName });
            //}
            //else
            //{
            //    tbRtData.Text = RtDataName;
            //}
            //if (tbPath.InvokeRequired)
            //{
            //    SetMessageCallback callback = new SetMessageCallback(this.Set_tbPathText);
            //    tbPath.BeginInvoke(callback, new string[] { Path });
            //}
            //else
            //{
            //    tbPath.Text = Path;
            //}

            //if (tbSysPostfix.InvokeRequired)
            //{
            //    SetMessageCallback callback = new SetMessageCallback(Set_tbSysPostfix);
            //    tbSysPostfix.BeginInvoke(callback, new string[] { Postfix});
            //}
            //else
            //{
            //    tbSysPostfix.Text = Postfix;
            //}

            //if (tbSysPostfix2.InvokeRequired)
            //{
            //    SetMessageCallback callback = new SetMessageCallback(Set_tbSysPostfix2);
            //    tbSysPostfix2.BeginInvoke(callback, new string[] { Postfix2});
            //}
            //else
            //{
            //    tbSysPostfix2.Text = Postfix2;
            //}

            //if (tbDevFile2.InvokeRequired)
            //{
            //    SetMessageCallback callback = new SetMessageCallback(this.Set_tbDevFileText2);
            //    tbDevFile2.BeginInvoke(callback, new string[] { DevFileName2 });
            //}
            //else
            //{
            //    tbDevFile2.Text = DevFileName2;
            //}
            //if (tbRtData2.InvokeRequired)
            //{
            //    SetMessageCallback callback = new SetMessageCallback(this.Set_tbRtDataText2);
            //    tbRtData2.BeginInvoke(callback, new string[] { RtDataName2 });
            //}
            //else
            //{
            //    tbRtData2.Text = RtDataName2;
            //}
            //if (tbPath2.InvokeRequired)
            //{
            //    SetMessageCallback callback = new SetMessageCallback(this.Set_tbPathText2);
            //    tbPath2.BeginInvoke(callback, new string[] { Path2 });
            //}
            //else
            //{
            //    tbPath2.Text = Path2;
            //}
            //this.Update();

            SendOrPostCallback callback = delegate
            {
                tbCoalMineName.Text = cfg.Name;// CoalMineName;
                tbCoalMineId.Text = cfg.Id.ToString( );// CoalMineId.ToString();
                tbIPAddress.Text = cfg.IPAddr;// ServerIp;
                tbInterval.Text = cfg.Interval.ToString( );// nInterval.ToString();
                tbKJType.Text = cfg.KJSysParas.KJType;//  KJType;
                tbPath.Text = cfg.KJSysParas.KJPath;// Path;
                tbDevFile.Text = cfg.KJSysParas.DevFile;// DevFileName;
                tbRtData.Text = cfg.KJSysParas.RtDataFile;// RtDataName;

                tbSysPostfix.Text = cfg.Postfix;
                tbSysPostfix2.Text = cfg.Postfix2;

                tbKJType2.Text = cfg.KJSysParas2.KJType;//  KJType;
                tbPath2.Text = cfg.KJSysParas2.KJPath;// Path;
                tbDevFile2.Text = cfg.KJSysParas2.DevFile;// DevFileName;
                tbRtData2.Text = cfg.KJSysParas2.RtDataFile;// RtDataName;
            };

            syncContext.Post( callback , null );
        }

        public delegate void SetMessageCallback( string s );

        protected void Set_tbKJTypeText( string s )
        {
            tbKJType.Text = s;
        }

        protected void Set_tbIPAddressText( string s )
        {
            tbIPAddress.Text = s;
        }

        protected void Set_tbCoalMineNameText( string s )
        {
            tbCoalMineName.Text = s;
        }

        protected void Set_tbIntervalText( string s )
        {
            tbInterval.Text = s;
        }

        protected void Set_tbCoalMineIdText( string s )
        {
            decimal d = 0;
            if ( decimal.TryParse( s , out d ) )
            {
                tbCoalMineId.Text = s;
            }
        }

        protected void Set_tbDevFileText( string s )
        {
            tbDevFile.Text = s;
        }

        protected void Set_tbRtDataText( string s )
        {
            tbRtData.Text = s;
        }

        protected void Set_tbPathText( string s )
        {
            tbPath.Text = s;
        }

        protected void Set_tbKJTypeText2( string s )
        {
            tbKJType2.Text = s;
        }

        protected void Set_tbDevFileText2( string s )
        {
            tbDevFile2.Text = s;
        }

        protected void Set_tbRtDataText2( string s )
        {
            tbRtData2.Text = s;
        }

        protected void Set_tbPathText2( string s )
        {
            tbPath2.Text = s;
        }

        protected void Set_tbSysPostfix( string s )
        {
            tbSysPostfix.Text = s;
        }

        protected void Set_tbSysPostfix2( string s )
        {
            tbSysPostfix2.Text = s;
        }
    }
}